package com.dhu.service.impl;

import com.dhu.dao.BarMapper;
import com.dhu.dao.UserBarFollowMapper;
import com.dhu.dao.UserBarOwnMapper;
import com.dhu.pojo.Bar;
import com.dhu.service.BarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BarServiceImpl implements BarService {

    @Autowired
    private BarMapper barMapper;
    @Autowired
    private UserBarFollowMapper userBarFollowMapper;
    @Autowired
    private UserBarOwnMapper userBarOwnMapper;

    // 创建贴吧
    @Override
    public int createBar(Bar bar) {
        try {
            int res = barMapper.createBar(bar);
            System.out.println(res);
            System.out.println(bar);
            // 获取新建bar的id
            int id = barMapper.selectIdByName(bar.getName());
            // user_bar表
            userBarFollowMapper.addFollow(bar.getHost(),bar.getId());
            userBarOwnMapper.addUserBarOwn(bar.getHost(),bar.getId());
            return res;
        } catch (Exception e) {
            return -1;
        }
    }

    @Override
    public List<Bar> selectAllBar() {
        try {
            return barMapper.selectAllBar();
        } catch (Exception e) {
            return null;
        }
    }

    // 返回用户关注的吧
    @Override
    public List<Bar> selectFollowBar(int id) {
        try {
            int[] idList = userBarFollowMapper.selectFollowBarId(id);
            return barMapper.selectFollowBar(idList);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public Bar selectBarById(int id) {
        try {
            return barMapper.selectBarById(id);
        } catch (Exception e) {
            return null;
        }
    }
}
